Skip to content

gfx/dmsh: specialize the blend fix-up away for pipelines whose blend words are both 0 (chain change) - #3176

Closed
dpadr wants to merge 3 commits into
seedds:mainfrom
dpadr:nutmeg/blend-off-fc
Closed

dpadr wants to merge 3 commits into
seedds:mainfrom
dpadr:nutmeg/blend-off-fc

Conversation

@dpadr

@dpadr dpadr commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Summary

dm_blend0 (the blend_zero fix-up) reads two words per draw: the zero-weight NaN rule (Ocarina of Time's mist) and NVIDIA's float32 rounding into normalized targets (Sonic Frontiers). For most draws both words are 0, yet every pixel shader still pays for the read, and it holds registers.

This adds a function constant, dm_blend_off (DMSH_BLEND_OFF_FC 217), that makes dm_blend0 return early.

  • When the host sets it: per pipeline, when the draw's words are both 0, in the same way as the border corrections.
    • pso_key.blend_live takes the key's spare word.
    • fn_off takes a mask (FN_OFF_BORDER, FN_OFF_BLEND).
  • Prewarm and offline pipelines (pre_fn, pso_offline.py) take the common specialization.
  • Mesh, feedback and wide-line pipelines keep the fix-up.
  • Pipelines with nonzero words read them as before, so the NaN and rounding cases are unchanged.
  • Reach: in Ocarina of Time, 89 % of draws, and every hot pixel shader.

This is a chain change: dmsh_chain.h is regenerated (26e1a5f38264 → 143cf4763549).

Measurements (Ocarina of Time, M2 Air, the same change on the cut9s base)

In-game A/B (bfab23b: 3 runs per arm, Kokiri route, cap 60, frame gen off). GPU at 1397–1398 MHz in both arms, 0 % of the time in the power zone.

Measure Change, B (specialized) − A (fix-up always)
Main 1440x810 pass −0.87 ms (95 % interval [−1.43, −0.30]; paired −0.89 [−1.53, −0.17])
Encoder sum −0.81 ms
GPU work −4.6 % (34.1 M → 32.6 M cycles a frame)
GPU energy −1 %
Frame p50 −0.65 ms (within noise)
  • [RED] lines are the same in both arms (only the known 9b310b33 mismatch).
  • The offline bench came first: fsbench on the hottest pixel shaders. Turning the words into constants ran as fast as removing the fix-up: −2.6 % / −0.7 % / −6.1 % on the three shaders with recorded scenes, bit-exact apart from up to 2.4e-7 relative.

Why there is no Ocarina of Time run on main: the A/B above ran on the cut9s base (chain f734e024449d). The change ported cleanly; the only adjustments were that bz_word / rop_word now take the lane map (g_pso_lmap) and the two tests load specialized functions. Ocarina of Time is not in the regress set (it has no Mac build there), and a fresh baseline pair on main would take about two hours of the shared Mac's queue for a result the cut9s A/B and the guard tests already cover.

Checks on this branch (a7710c4)

  • Host tests: t_nvn 506 passed, 0 failed; t_pso_skey OK.
  • Guards, both still active where their words are nonzero:
    • t_blend_zero: 197 / 0.
    • t_rop_round: 9284 / 0.
    • Both pass with each guard forced on and forced off. t_rop_round covers Sonic Frontiers' rounding case; the game itself isn't available locally.
  • scripts/regress.py check --games against 656f884 (this branch's base):
game status shaders (base → cand) newly failing newly building changed MSL interface (new / known) boot fps p50 frame p99 ms peak MB prepare s new RED
SR 0400380020056000 CHANGED 27190/27190 → 27190/27190 0 0 16079 0 / 0 – – – – – –
Animal Well 010020D01AD24000 CHANGED 46/46 → 46/46 0 0 45 0 / 0 ok 59.8 → 59.7 17.15 → 17.23 993 → 995 0.49 → 0.42 0
UFO 50 01004F90239BA000 PASS 59/59 → 59/59 0 0 0 – – – – – – –
  • Why these games: SR and Animal Well are the local NVN games with Mac builds; changed MSL is expected, since every pixel shader gains the constant. UFO 50 is a GL control.
  • Mina was left out: its local build's shader package predates keyed records and needs a re-split before regress can build it. Unrelated to this change.
  • Ocarina of Time and DKB have no Mac build in the regress set.
  • A regress.py bug this exposed: the baseline's and candidate's pso_offline.py share one dmsh_abi module in a single process. On current regress.py, a candidate that adds a dmsh_abi constant fails the interface check with the baseline's header. The table above was produced with that fixed locally; the fix will be sent as its own PR.

🤖 Generated with Claude Code

@github-actions github-actions Bot added nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) shader chain The shader translation chain (DMSH_CHAIN): a change rebuilds every game's shaders labels Nov 21, 2026
seedds and others added 2 commits November 21, 2026 23:54
…words are both 0 (chain change)

dm_blend0 (dmsh_rewrite.py blend_zero) reads two words per draw: the zero-weight NaN rule
and NVIDIA's float32 rounding into normalized targets. For most draws both are 0, and the
read alone cost 2-6 % of Ocarina of Time's hottest pixel shaders on an M2 (fsbench: the
words as constants run as fast as no fix-up at all).

A new function constant dm_blend_off (DMSH_BLEND_OFF_FC 217) makes dm_blend0 return first.
The host sets it per pipeline when the draw's words are both 0 (pso_key.blend_live, the
key's former spare word; fn_off now takes a mask: FN_OFF_BORDER, FN_OFF_BLEND), as it does
for the border corrections. That is 89 % of Ocarina of Time's draws and every hot pixel
shader. Prewarm (pre_fn) and offline pipelines (pso_offline.py) take the common
specialization; mesh, feedback and wide-line pipelines keep the fix-up. Pipelines with
nonzero words read them as before, so the NaN and rounding cases are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ialized, as shader_load does

The blend helper now declares dm_blend_off, and Metal builds no pipeline from a function
with constants that was not specialized (shader_load specializes every such function with
empty values; the tests took the plain function).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
pso_bg_look's caller still passed the border-only flag (!qk->border), so a prediction
looked up and built its base without dm_blend_off while the draw asks for pso_off(k):
the worker never found the base the draw would use, and the draw compiled again.
t_pso_bg_look builds its pulling base as a draw does (pso_off), and t_border's fn_off
call takes the mask.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@dpadr
dpadr force-pushed the nutmeg/blend-off-fc branch from a7710c4 to f0b0e1c Compare November 22, 2026 08:14
@dpadr

dpadr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main (983a9ab), plus one fix.

  • Chain: regenerated, dmsh_chain.h 143cf4763549 → 1ae20f269d98. Main's chain had moved with gfx/tools: pass subgroup inputs by value to out-of-line mesh helpers #3169 (mesh subgroup inputs). Mesh pipelines keep the blend fix-up in this change, so the move doesn't touch what the measurements rest on.
  • The fix (f0b0e1c): main added background base lookups for predicted pipelines (pso_bg_look), and the port still passed them the border-only flag !qk->border.
    • A prediction looked up and built its base without dm_blend_off, while the draw asks for pso_off(k). The base was never found, and the draw compiled again.
    • It now passes pso_off(qk).
    • t_pso_bg_look caught it once its pulling base was built the way a draw builds it.
    • t_border also called fn_off with one argument: that was the build and clang-tidy failure.
  • What the measurements rest on: the in-game A/B ran on a base without pso_bg_look, so the bug never ran there. The numbers stand as stated.
  • Local tests on f0b0e1c:
    • t_border 113/0, t_pso_bg_look 17/0, t_nvn 506/0.
    • Every t_pso_* suite: 0 failed.
    • t_blend_zero 197/0 and t_rop_round 9284/0, each with the guard forced on and off.
    • test_nsp_chain, test_dmsh_blend_zero, test_pso_offline, test_docs: OK.
  • Failing on main as well, not from this change (each also fails on main 806fabe locally): t_gl_imports (test_lod_bias), t_logic, t_missing_src1, t_nvn_accel, t_nvn_accel_sbt, t_rt_recycle. t_image_alias is flaky on main.
  • CI's Tool tests failures (test_patches, test_nvn_rt_api) appeared on gfx/nvn: GPUPROF watchdog: drop a periodic profile whose report never came #3160 too, which touches neither area. Both pass locally on current main.

seedds added a commit that referenced this pull request Nov 22, 2026
…(PR #3190)

interface_check was the only place in scripts/regress.py that loaded a revision tree's Python tool in-process via load_module. Because pso_offline.py imports dmsh_abi, dmsh_side, romfs, shader_lib and vol_space by name, the baseline and candidate shared sys.modules (and got the harness checkout's romfs), causing a candidate that adds a dmsh_abi constant (such as PR #3176's DMSH_BLEND_OFF_FC) to fail with AttributeError against the baseline's header and risking silently masking helper differences.

Run pso_offline.read_pairs/pair_text and (when suspects exist) pso_offline.plan through Tree.py with JSON over stdin instead, matching how regress.py already isolates dmsh_chain and game.py.

Co-authored-by: David Padron-Harris <34929160+dpadr@users.noreply.github.com>
seedds added a commit that referenced this pull request Nov 23, 2026
…0 (PR #3176)

Add function constant dm_blend_off (DMSH_BLEND_OFF_FC 217) to dm_blend0 so pipelines whose blend words (bz_word and rop_word) are both 0 compile out the buffer read and fix-up tail (-0.87 ms main pass, -4.6% GPU cycles on Ocarina of Time, M2 Air).

Revises PR #3176 in three ways:
- Derive pso_blend_live / pso_off directly from (pso_key, fs, merged) via bz_word_for / rop_word_for instead of adding a redundant pso_key field or reading global g_pass.cfmt / g_pso_lmap in pso_key_fill (fixes fsp_plan, fb_tile_ready, pso_predict_rest_, and preserves all existing pso_manifest.tsv / pso_user.tsv R and M rows).
- Keep dm_blend_off = false in pso_offline.py, pre_fn, shader_load, and hybrid_get / pso_bg_look pulling lookups so 100% of bundled pair-archive pulling stand-ins remain valid across all blend states, while steady-state pipelines specialize dm_blend_off = true when both words are 0.
- Add GPU and bundle tests for FN_OFF_BLEND, combined FN_OFF_BORDER | FN_OFF_BLEND, k->cfmt != g_pass.cfmt, and offline pair-archive lookup.

Co-authored-by: David Padron-Harris <34929160+dpadr@users.noreply.github.com>
@seedds

seedds commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Thank you @dpadr for the diagnosis, the clean function-constant approach, and the M2 A/B measurements!

Landed on main in 3cf7506 (co-authored with you) with a few adjustments before merge:

  • Instead of storing a separate blend_live flag in the pipeline key (which would cause existing prewarm lists to miss on draws with active blend/rounding words) and reading the current pass's target formats during key setup, the host now computes whether the blend fix-up is needed directly from the pipeline key's own target formats, blend state, and lane map. That keeps all existing prewarm manifests valid and ensures predicted, face-split, and tile-reading pipelines always get the right specialization.
  • Kept the offline pair archive and temporary hybrid stand-ins with the blend fix-up enabled (dm_blend_off = false), while steady-state pipelines still compile it out whenever both words are zero. That preserves full offline archive coverage for hybrid stand-ins across games where many shader pairs draw to normalized or blended float targets, while keeping the full steady-state GPU speedup.
  • Added GPU and archive tests covering all specialization combinations and verifying that forcing the fix-up off compiles out both the NaN guard and normalized rounding.

Checked locally on an Apple M6 Mac (host build, GPU blend/rounding/border/pipeline suites under Metal validation, and chain/archive/docs unit tests). Offline Metal compiler tests (t_pso_bundle, shader compile checks, and regress.py) and CI linters were not run locally due to a local Xcode/MetalToolchain mismatch and are left for GitHub CI to verify.

@seedds seedds closed this Nov 23, 2026
Kayyluhh added a commit to Kayyluhh/nxnative that referenced this pull request Nov 23, 2026
…nk's probation (3d186f2) beside the batch's latency rules (seedds#1717's 30 Hz link, main's fine mode); chain f19baf01187e -> 4dbd705d26e9 (seedds#3176's blend fix-up); symptom index regenerated
Aznracer added a commit to Aznracer/Mighty-Green-Kokiri-Kid that referenced this pull request Nov 23, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Claude-Session: https://claude.ai/code/session_01WGrCTWig49u9S8ZCH37TUc
Aznracer pushed a commit to Aznracer/Mighty-Green-Kokiri-Kid that referenced this pull request Nov 23, 2026
The 16:9 window crop is taken at the screen's shape in a texture of
that shape. DM_NVN_BLEND_SPEC=0 keeps prewarm rows matching (seedds#3176
made draws wait). Memory pressure mode from 1200 MB of room.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WGrCTWig49u9S8ZCH37TUc
Aznracer pushed a commit to Aznracer/Mighty-Green-Kokiri-Kid that referenced this pull request Nov 23, 2026
…0 (PR seedds#3176)

Add function constant dm_blend_off (DMSH_BLEND_OFF_FC 217) to dm_blend0 so pipelines whose blend words (bz_word and rop_word) are both 0 compile out the buffer read and fix-up tail (-0.87 ms main pass, -4.6% GPU cycles on Ocarina of Time, M2 Air).

Revises PR seedds#3176 in three ways:
- Derive pso_blend_live / pso_off directly from (pso_key, fs, merged) via bz_word_for / rop_word_for instead of adding a redundant pso_key field or reading global g_pass.cfmt / g_pso_lmap in pso_key_fill (fixes fsp_plan, fb_tile_ready, pso_predict_rest_, and preserves all existing pso_manifest.tsv / pso_user.tsv R and M rows).
- Keep dm_blend_off = false in pso_offline.py, pre_fn, shader_load, and hybrid_get / pso_bg_look pulling lookups so 100% of bundled pair-archive pulling stand-ins remain valid across all blend states, while steady-state pipelines specialize dm_blend_off = true when both words are 0.
- Add GPU and bundle tests for FN_OFF_BLEND, combined FN_OFF_BORDER | FN_OFF_BLEND, k->cfmt != g_pass.cfmt, and offline pair-archive lookup.

Co-authored-by: David Padron-Harris <34929160+dpadr@users.noreply.github.com>
(cherry picked from commit 3cf7506)
Aznracer pushed a commit to Aznracer/Mighty-Green-Kokiri-Kid that referenced this pull request Nov 23, 2026
…y default); blend spec on the merged seedds#3176 form

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WGrCTWig49u9S8ZCH37TUc
Aznracer pushed a commit to Aznracer/Mighty-Green-Kokiri-Kid that referenced this pull request Nov 25, 2026
…(PR seedds#3190)

interface_check was the only place in scripts/regress.py that loaded a revision tree's Python tool in-process via load_module. Because pso_offline.py imports dmsh_abi, dmsh_side, romfs, shader_lib and vol_space by name, the baseline and candidate shared sys.modules (and got the harness checkout's romfs), causing a candidate that adds a dmsh_abi constant (such as PR seedds#3176's DMSH_BLEND_OFF_FC) to fail with AttributeError against the baseline's header and risking silently masking helper differences.

Run pso_offline.read_pairs/pair_text and (when suspects exist) pso_offline.plan through Tree.py with JSON over stdin instead, matching how regress.py already isolates dmsh_chain and game.py.

Co-authored-by: David Padron-Harris <34929160+dpadr@users.noreply.github.com>
(cherry picked from commit e531be8)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

nvn NVN on Metal and its tests (dm/gfx/nvn, dm/gfx/tests) shader chain The shader translation chain (DMSH_CHAIN): a change rebuilds every game's shaders

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants